﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Win32;

namespace vpn
{
    class CountBomb
    {
        static private int TotalTime = Consts.MaxTrailCnt;

        static public int GetCurrentCnt()
        {
            try
            {
                RegistryKey software = Registry.CurrentUser.OpenSubKey(/*NOXLATE*/"Software");
                RegistryKey vpnHelper = software.OpenSubKey("VpnHelper");
                if (null == vpnHelper)//first time run
                {
                    SetCurrentCnt(0);
                    return 0;
                }
                string encryptCnt = vpnHelper.GetValue("cnt").ToString();
                vpnHelper.Close();
                int cnt = Int32.Parse(encryptCnt);
                return cnt;
            }
            catch(Exception e)
            {
                return -1;
            }
        }
        static public bool SetCurrentCnt(int cnt)
        { 
            try
            {
                RegistryKey software = Registry.CurrentUser.OpenSubKey(/*NOXLATE*/"Software",true);
                RegistryKey vpnHelper = software.CreateSubKey("VpnHelper");

                vpnHelper.SetValue("cnt", cnt, RegistryValueKind.String);
                vpnHelper.Close();
                return true;
            }
            catch(Exception e)
            {
                return false;
            }
        }

        static public bool Validate()
        { 
            int currentCnt = GetCurrentCnt();
            if (currentCnt == -1 || currentCnt > TotalTime)
                return false;

            SetCurrentCnt(++currentCnt);
            return true;
        }

        static public int GetLastTimes()
        { 
        
            return  TotalTime - GetCurrentCnt();
        }
    }
}
